package com.ubao.activity;

import java.util.HashMap;
import java.util.Map;

import com.ubao.R;
import com.ubao.base.RequestBaseActivity;
import com.ubao.bean.Status;
import com.ubao.network.JsonParser;
import com.ubao.network.ResponseInterface;
import com.ubao.network.Url;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;
import com.ubao.ui.titlebar.GTitleBar;
import com.ubao.util.Util;
import com.ubao.util.log.Logger;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class ActivateActivity extends RequestBaseActivity implements OnClickListener {

	EditText etLoginEmail;
	EditText etNickName;
	EditText etPassword;
	EditText etPasswordRe;
	GTitleBar gTitleBar;
	String email = "";
	String nickname = MyApp.weiboUser.sina_name;
	String password = "";
	String passwordRe = "";
	String uid = MyApp.weiboUser.sina_id;
	String isreg = "0";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_activate);
		initViews();
	}

	private void initViews() {
		etLoginEmail = (EditText) findViewById(R.id.etLoginEmail);
		etNickName = (EditText) findViewById(R.id.etNickName);
		etPassword = (EditText) findViewById(R.id.etPassword);
		etPasswordRe = (EditText) findViewById(R.id.etPasswordRe);
		etNickName.setText(MyApp.weiboUser.sina_name);
		
		initTitleBar();
	}
	
	private void initTitleBar() {
		gTitleBar = (GTitleBar) findViewById(R.id.mTitleBar);
		gTitleBar.initRightButton("跳过", R.drawable.titlebar_button_bg, this);
	}
	
	public void confirm(View view){
		getData();
		if(!invalidate()){
			return;
		}
		sendRequest(Url.getParams(Url.url_register,getMap()));
		
	}

	private void getData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		
		if(v.getId()==R.id.rightButton){
			email = "";
			nickname = MyApp.weiboUser.sina_name;
			password = "";
			passwordRe = "";
			uid = MyApp.weiboUser.sina_id;
			isreg = "0";
			sendRequest(Url.getParams(Url.url_register,getMap()));
			Intent intent = new Intent(this,MainActivity.class);
			startActivity(intent);
		}
	}
	
	private boolean invalidate() {
		email = etLoginEmail.getText().toString().trim();
		nickname = etNickName.getText().toString().trim();
		password = etPassword.getText().toString().trim();
		passwordRe = etPasswordRe.getText().toString().trim();
		if(!Util.validateEmail(email)){
			showToast("请正确填写email");
			return false;
		}
		if(nickname.equals("")){
			showToast("请填写昵称");
			return false;
		}
		if(!password.equals(passwordRe)){
			showToast("两次输入密码不相同");
			return false;
		}
		if(password.length()<6||password.length()>20){
			showToast("密码长度为6-20位");
			return false;
		}
		isreg = "1";
		return true;
	}
	
	
	
	private Map getMap(){
		Map<String, String> para = new HashMap<String, String>();
    	para.put("userid", uid);
    	para.put("password", password);
    	para.put("username", nickname);
    	para.put("email", email);
    	para.put("token", Util.getDeviceId(this));
    	para.put("isreg", isreg);
    	para.put("gender", MyApp.weiboUser.getSinaGender());
    	return para;
	}
	
	private void sendRequest(String params){
		startProgress();
		SpecialRequest request = new SpecialRequest(Url.url_register_full);
		request.encoding = "utf-8";
		request.postParams = params;
		mHttpHandler.sendRequest(request);
	}

	@Override
	protected void httpCompleted(ResponseInterface resp) {
		SpecialResponse response = (SpecialResponse)resp;
		Status s = JsonParser.getStatus(response.content);
		if(!s.getStatus().equals("200")){
			Message msg = handler.obtainMessage();
			msg.obj = s.getMessage();
			msg.what = 0;
			handler.sendMessage(msg);
		}else{
			handler.sendEmptyMessage(1);
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub
		
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			closeProgress();
			switch(msg.what){
			case 0:
				showToast((String)msg.obj);
				break;
			case 1:
				Intent intent = new Intent(ActivateActivity.this,MainActivity.class);
				startActivity(intent);
				break;
			}
		};
	};
	
}
